package com.atguigu.crowd.interceptor;

import com.atguigu.crowd.exception.AccessForbiddenException;
import com.atguigu.crowd.util.CrowdConstant;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.security.auth.login.AccountExpiredException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        HttpSession session = request.getSession();

        Object adminObj = session.getAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN);

        if (adminObj == null) {
            throw new AccessForbiddenException(CrowdConstant.MESSAGE_ACCESS_FORBIDDEN);
        } else {
            return true;
        }
    }


}
